package com.scuec.groupfive.Controller;

import com.scuec.groupfive.service.DailyPracticesService;
import com.scuec.groupfive.vo.DailyPracticesVO;
import com.scuec.groupfive.vo.AnswerResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 每日练习Controller
 */
@RestController
@RequestMapping("/api/daily-practices")
public class DailyPracticesController {

    @Autowired
    private DailyPracticesService dailyPracticesService;

    /**
     * 获取当前用户的每日练习题目
     * @param userId 用户ID
     * @return 每日练习题目列表
     */
    @GetMapping
    public List<DailyPracticesVO> getDailyPractices(@RequestParam Long userId) {
        return dailyPracticesService.getDailyPracticesByUserId(userId);
    }

    /**
     * 提交每日练习答案
     * @param request 包含userId和answers的请求体
     * @return 批改结果列表
     */
    @PostMapping("/submit")
    public List<AnswerResultVO> submitAnswers(@RequestBody Map<String, Object> request) {
        Long userId = Long.valueOf(request.get("userId").toString());
        List<Map<String, Object>> answers = (List<Map<String, Object>>) request.get("answers");
        return dailyPracticesService.submitAnswers(userId, answers);
    }

    /**
     * 获取每日练习记录
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param keyword 关键字
     * @param page 页码
     * @param size 每页大小
     * @return 每日练习记录列表
     */
    @GetMapping("/records")
    public List<Map<String, Object>> getDailyPracticeRecords(
            @RequestParam Long userId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return dailyPracticesService.getDailyPracticeRecords(
                userId, startDate, endDate, keyword, page, size);
    }

    /**
     * 获取每日练习结果详情
     * @param userId 用户ID
     * @param practiceId 练习日期
     * @return 练习结果详情列表
     */
    // [file name]: DailyPracticesController.java
// 修改 getPracticeResults 方法
    @GetMapping("/results")
    public List<AnswerResultVO> getPracticeResults(
            @RequestParam Long userId,
            @RequestParam String practiceId) {
        // 从 practiceId 中提取日期 (格式: "daily_2023-01-01")
        String practiceDate = practiceId.replace("daily_", "");
        return dailyPracticesService.getPracticeResults(userId, practiceDate);
    }

    /**
     * 更新答题记录的收藏状态
     * @param request 请求参数
     * @return 操作结果
     */
    @PutMapping("/favorite")
    public ResponseEntity<?> updateFavoriteStatus(@RequestBody Map<String, Object> request) {
        Long recordId = Long.valueOf(request.get("recordId").toString());
        int isFavorite = Integer.parseInt(request.get("isFavorite").toString());
        Long userId = Long.valueOf(request.get("userId").toString());
        Long questionId = Long.valueOf(request.get("questionId").toString());

        boolean success = dailyPracticesService.updateFavoriteStatus(
                recordId, isFavorite, userId, questionId
        );

        if (success) {
            return ResponseEntity.ok().body(Collections.singletonMap("message", "收藏状态更新成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Collections.singletonMap("error", "收藏状态更新失败"));
        }
    }

}